// pages/agent/mymoney/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commission: {
      istrue: true,
      record: 11,
      money: 235
    },
    isDuaration: true,
    isCount: '',
    isBank: '',
    isbank_account: '',
    isCashmoney: ''
  },
  // 卡号
  countVal(e) {
    this.setData({
      isCount: e.detail.value
    })
    // console.log(e.detail.value)
  },
  // 开户行
  bankVal(e) {
    this.setData({
      isBank: e.detail.value
    })
    // console.log(e.detail.value)
  },
  // 添加银行账户
  addCount() {
    let _this = this
    let bank_account = _this.data.isCount
    let bank_name = _this.data.isBank
    let open_id = wx.getStorageSync('user_new')
    if (bank_account == '' || bank_name == '') {
      wx.showToast({
        title: '带 * 为必填',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else if (!(/^([1-9]{1})(\d{14}|\d{18})$/.test(bank_account))) {
      wx.showToast({
        title: '银行卡号格式错误！请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    else {
      let bank_account = _this.data.isCount
      let bank_name = _this.data.isBank
      let open_id = wx.getStorageSync('user_new')
      app.core('post', 'addAdviserApplyAccount', {
        'bank_account': bank_account,
        'bank_name': bank_name,
        'open_id': open_id,
      }, (res) => {
        console.log(res)
        if (res.resultCode == 200) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 2000
          });
          this.onLoad()
        } else {
          wx.showToast({
            title: '网络出错！',
            icon: 'none',
            duration: 2000
          });
        }
      },
        (err) => {
          console.log(err)
        }
      )

    }
  },
  Cashwithdrawal() {
    let open_id = wx.getStorageSync('user_new')
    let isCashmoney = this.data.isCashmoney
    // console.log(isCashmoney)
    // return false
    if (isCashmoney === '0.00') {
      wx.showToast({
        title: '可提现金额为零',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    app.core('get', 'adviserApplyCash', {
      'open_id': open_id,
      'money': isCashmoney
    }, (res) => {
      console.log(res)
      if (res.resultCode == 200) {
        wx.showToast({
          title: '提现申请提交成功',
          icon: 'success',
          duration: 2000
        });
        wx.navigateTo({
          url: '/pages/adviser/mymoney/mymoney',
        })
      } else {
        wx.showToast({
          title: '网络出错！',
          icon: 'none',
          duration: 2000
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    setTimeout(() => {
      this.setData({
        isDuaration: false,

      })
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let open_id = wx.getStorageSync('user_new')
    app.core('get', 'findAdviserByOpenid?open_id=' + open_id, '', (res) => {
      console.log(res)
      if (res.resultCode === 200) {
        this.setData({
          commission: res.data,
          isbank_account: res.data.bank_account,
          isCashmoney: res.data.left_cash
        })
      }
    })
  },
  toMymoney() {
    wx.navigateTo({
      url: '/pages/adviser/mycust/mycust',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})